﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KPIS.GERP.Models;
using KPIS.GERP.Models.SYS;
using KPIS.GERP.OMVC.Classes;

namespace KPIS.GERP.OMVC.Controllers
{
    public class ApproveProcessGroupController : Controller
    {
        private GERPContext context = new GERPContext(true);
        private int empId {get { return new SsMng().EmpSeqI; }}
        public ActionResult Index()
        {
            return View();
        }
        
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(ApproveProcessGroup model)
        {
            ValidateModel(model);
            if(!ModelState.IsValid){
                return View(model);
            } 
            model.RecordStatus = "U";
            model.CreatedBy = empId;
            model.CreatedWhen = DateTime.Now;
            model = context.ApproveProcessGroups.Add(model);
            context.SaveChanges();
            return RedirectToAction("Detail", new {id = model.AppPrcGrpId});
        }
        
        public ActionResult Update(int id)
        {
            return View(context.ApproveProcessGroups.FirstOrDefault(o=>o.AppPrcGrpId == id));
        }

        [HttpPost]
        public ActionResult Update(ApproveProcessGroup model)
        {
            ValidateModel(model);
            if(!ModelState.IsValid){
                return View(model);
            } 
            context.ApproveProcessGroups.Attach(model);
            context.Entry(model).State = EntityState.Modified;
            model.UpdatedBy = empId;
            model.UpdatedWhen = DateTime.Now;
            context.SaveChanges();
            return RedirectToAction("Detail", new {id = model.AppPrcGrpId});
        }
        
        public ActionResult Detail(int id)
        {
            var model = context.ApproveProcessGroups.FirstOrDefault(o => o.AppPrcGrpId == id);
            if(model ==  null) return HttpNotFound();

            model.LoadMoreInformation();
            return View(model);
        }

        public ActionResult Delete(int id)
        {
            context.ApproveProcessGroups.FirstOrDefault(o => o.AppPrcGrpId == id).RecordStatus = "D";
            context.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult Enable(int id)
        {
            context.ApproveProcessGroups.FirstOrDefault(o => o.AppPrcGrpId == id).RecordStatus = "U";
            context.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult SetDefault(int appPrcId)
        {
            var appPrc = context.ApproveProcesses.First(o => o.AppPrcId == appPrcId);
            foreach(var appPrcEach in context.ApproveProcesses.Where(o=>o.RecordStatus != "D" && o.AppPrcGrpId == appPrc.AppPrcGrpId))
            {
                appPrcEach.IsDefaultProcess = false;
            }
            appPrc.IsDefaultProcess = true;
            context.SaveChanges();
            return RedirectToAction("Detail", new
                {
                    id = appPrc.AppPrcGrpId
                });
        }

        public ActionResult SetProcessToEntity(int appPrcGrpId,int entityid, int? appPrcId = null)
        {
            var appPrcGrp = context.ApproveProcessGroups.First(o=>o.AppPrcGrpId == appPrcGrpId);
            appPrcGrp.LoadMoreInformation();
            appPrcGrp.SetAppPrcIdToEntity(appPrcId, entityid);
            return RedirectToAction("Detail", new {id=appPrcGrpId});
        }

        public void ValidateModel(ApproveProcessGroup model)
        {
            if(string.IsNullOrWhiteSpace(model.Name))
            {
                ModelState.AddModelError("Name", "กรุณาใส่ชื่อ");
            }
        }
    }
}
